<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;
use think\Request;
use app\admin\model\UserModel;

class User extends Common
{
	/**
	*	会员列表
	*/
	public function index()
	{
		$data = $this->request->param();
		$UserModel = new UserModel();
		$list = $UserModel->getUser($data);
		// 获取分页显示
	    $page = $list->render();
	    $this->assign('page',$page);
		$this->assign('list',$list);

		$this->assign('u_type',isset($data['u_type']) ? $data['u_type'] : '');
		$this->assign('u_name',isset($data['u_name']) ? $data['u_name'] : '');
		$this->assign('u_card',isset($data['u_card']) ? $data['u_card'] : '');
		$this->assign('u_tel',isset($data['u_tel']) ? $data['u_tel'] : '');
		$this->assign('u_store',isset($data['u_store']) ? $data['u_store'] : '');
		$this->assign('keyword',isset($data['keyword']) ? $data['keyword'] : '');
		return $this->fetch();
	}

	/**
	*	添加会员
	*/
	public function add()
	{
		return $this->fetch();
	}

	/**
	*	添加会员提交
	*/
	public function addPost()
	{
		if ($this->request->isPost()) {
			$data = $this->request->param();
			$result = $this->validate($data, 'UserValidate');

	        if ($result !== true) {

	            $this->error($result);
	        }

			$UserModel = new UserModel();

	        $list = $UserModel->addUser($data);

	        if ($list){
	        	$this->success('添加成功');
	        }else{
	        	$this->error('添加失败');
	        }
        }
	}

	/**
	*	修改会员信息
	*/
	public function edit()
	{
		$data = $this->request->param();

		if($data){
			$UserModel = new UserModel();
			$list = $UserModel->getOne($data['id']);
			$this->assign('list',$list);
		}
		return $this->fetch();
	}

	/**
	*	修改会员信息提交
	*/
	public function editPost()
	{
		if ($this->request->isPost()) {
			$data = $this->request->param();
			$result = $this->validate($data, 'UserValidate');

	        if ($result !== true) {

	            $this->error($result);
	        }

			$UserModel = new UserModel();

	        $list = $UserModel->editUser($data['id'],$data);

	        $this->success('修改成功','/admin/User/index');
        }
	}


	/**
	*	合伙人列表
	*/
	public function partner()
	{
		$data = $this->request->param();
		$UserModel = new UserModel();
		$list = $UserModel->getPartner($data);
		// 获取分页显示
	    $page = $list->render();
	    $this->assign('page',$page);
		$this->assign('list',$list);

		$this->assign('u_name',isset($data['u_name']) ? $data['u_name'] : '');
		$this->assign('u_card',isset($data['u_card']) ? $data['u_card'] : '');
		$this->assign('u_tel',isset($data['u_tel']) ? $data['u_tel'] : '');
		$this->assign('u_store',isset($data['u_store']) ? $data['u_store'] : '');
		$this->assign('keyword',isset($data['keyword']) ? $data['keyword'] : '');
		return $this->fetch();
	}


	/**
	*	修改合伙人信息
	*/
	public function editPartner()
	{
		$data = $this->request->param();

		if($data){
			$UserModel = new UserModel();
			$list = $UserModel->getPartnersOne($data['id']);
			$this->assign('list',$list);
		}
		return $this->fetch();
	}


	/**
	*	修改合伙人信息提交
	*/
	public function editPartnerPost()
	{
		if ($this->request->isPost()) {
			$data = $this->request->param();
			$result = $this->validate($data, 'PartnersValidate');

	        if ($result !== true) {

	            $this->error($result);
	        }

			$UserModel = new UserModel();

	        $list = $UserModel->editPartner($data['id'],$data);

	        $this->success('修改成功','/admin/User/partner'); 
        }
	}

	/**
	*	删除合伙人信息
	*/
	public function deletePartner()
	{
		$data = $this->request->param();
		if($data){
			$UserModel = new UserModel();
			$list = $UserModel->deleteUser($data['id']);
			if($list){
				$this->success('删除成功','/admin/User/partner');
			}else{
				$this->error('删除失败');
			}
		}
	}

	/**
	*	删除会员信息
	*/
	public function delete()
	{
		$data = $this->request->param();
		if($data){
			$UserModel = new UserModel();
			$list = $UserModel->deleteUser($data['id']);
			if($list){
				$this->success('删除成功','/admin/User/index');
			}else{
				$this->error('删除失败');
			}
		}
	}

	/**
	*	每个会员的收货地址
	*/
	public function indexAddress()
	{
		$data = $this->request->param();
		if($data){
			$UserModel = new UserModel();
			$user = $UserModel->getOne($data['id']);
			$list = $UserModel->getUsersAddress($data['id']);
			
			// 获取分页显示
		    $page = $list->render();
		    $this->assign('page',$page);
		    $this->assign('user',$user);
			$this->assign('list',$list);
		}

		return $this->fetch();
	}

	/**
	*	修改每个会员的收货地址
	*/
	public function editAddress()
	{
		$data = $this->request->param();
		if($data){
			$UserModel = new UserModel();
			$list = $UserModel->getAddress($data['id']);
			$user = $UserModel->getOne($data['uid']);
			$this->assign('user',$user);
			$this->assign('list',$list);
		}
		return $this->fetch();
	}

	/**
	*	修改每个会员的收货地址提交
	*/
	public function editAddressPost()
	{
		if ($this->request->isPost()) {
			$data = $this->request->param();

			$UserModel = new UserModel();

	        $list = $UserModel->editUserAddress($data['id'],$data);
	        $this->success('修改成功',url('/admin/User/indexAddress',['id'=>$data['uid']]));
	        
        }
	}

	/**
	*  删除会员的收货地址
	*/
	public function deleteAddress()
	{
		$data = $this->request->param();

		if($data){
			$UserModel = new UserModel();
			$list = $UserModel->deleteAddress($data['id']);
			if($list){
				$this->success('删除成功',url('/admin/User/indexAddress',['id'=>$data['uid']]));
			}else{
				$this->error('删除失败');
			}
		}
	}
}